package com.realgodo.site.web;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springside.modules.orm.Page;

import com.realgodo.site.entity.site.News;
import com.realgodo.site.service.site.NewsManager;

@Scope("prototype")
@Results( { @Result(name = "tobase", type = "redirect", location = "${base}/") })
public class NewsController extends ControllerSupport {

	private static final long serialVersionUID = -1797448875647737753L;

	private Long id;

	private News news;

	private Page<News> newsPage = new Page<News>(20);

	@Resource
	private NewsManager newsManager;

	public String index() {
		newsPage
				.setPageNo(newsManager.isParamNotNull(getParamMap(), Page.PAGE) ? Integer
						.valueOf((String) newsManager.getParamValue(
								getParamMap(), Page.PAGE))
						: 1);// 查询页数
		newsPage = this.newsManager.findNews(newsPage, null, News.PUBLISHED,
				true);
		super.setPageInfo(newsPage);
		return "news";
	}

	public String show() throws Exception {
		if (null != getId()) {
			news = this.newsManager.get(getId());
		} else {
			return "tobase";
		}
		return "show";
	}

	/** --------------------getter and setter------------------- */
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public Page<News> getNewsPage() {
		return newsPage;
	}

	public void setNewsPage(Page<News> newsPage) {
		this.newsPage = newsPage;
	}

}
